package com.deluxelx.rmo.essay.domain.rule.logic;

import com.deluxelx.rmo.common.constants.DecisionLimitType;
import com.deluxelx.rmo.essay.domain.rule.engine.EngineConfig;
import com.deluxelx.rmo.essay.model.bo.TreeNodeLine;
import com.deluxelx.rmo.essay.model.req.DecisionMakingReq;

import java.util.List;

import static com.deluxelx.rmo.common.constants.TreeConstants.TREE_NULL_NODE;

/**
 * 规则过滤器基础抽象类
 */
public abstract class BaseLogic implements LogicFilter, EngineConfig {

    @Override
    public Long decisionMaking(String decisionValue, List<TreeNodeLine> treeNodeLineList) {
        for (TreeNodeLine nodeLine: treeNodeLineList) {
            if (this.decisionLogic(decisionValue, nodeLine)) {
                return nodeLine.getNodeIdTo();
            }
        }
        return TREE_NULL_NODE;
    }

    @Override
    public abstract String getDecisionValue(DecisionMakingReq decisionMakingReq);

    /**
     * 决策逻辑（根据决策值和一条相连链路，根据相连链路里的 limitType 和 limitValue 来判定是否选择这一条链路走下去）
     * @param decisionValue 决策值
     * @param nodeLine 与节点相连的链路
     * @return 判断这条 nodeLine 是否是（通过决策）的下一条路
     */
    private boolean decisionLogic(String decisionValue, TreeNodeLine nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case DecisionLimitType.EQUAL:
                return decisionValue.equals(nodeLine.getRuleLimitValue());
            case DecisionLimitType.GT:
                return Double.parseDouble(decisionValue) > Double.parseDouble(nodeLine.getRuleLimitValue());
            case DecisionLimitType.LT:
                return Double.parseDouble(decisionValue) < Double.parseDouble(nodeLine.getRuleLimitValue());
            case DecisionLimitType.GE:
                return Double.parseDouble(decisionValue) >= Double.parseDouble(nodeLine.getRuleLimitValue());
            case DecisionLimitType.LE:
                return Double.parseDouble(decisionValue) <= Double.parseDouble(nodeLine.getRuleLimitValue());
            case DecisionLimitType.RANGE:
                // 是否在一个区间中
                String[] ruleLimitValueArr = nodeLine.getRuleLimitValue().split("-");
                return Double.parseDouble(decisionValue) >= Double.parseDouble(ruleLimitValueArr[0])
                        && Double.parseDouble(decisionValue) <= Double.parseDouble(ruleLimitValueArr[1]);
            default:
                return false;
        }
    }
}
